<?php
/**

 //  DoYouHaoBaby-X Ucenter ( DoYouHaoBaby-X 计划 )
 //  +---------------------------------------------------------------------
 //
 //  “Copyright”
 //  +---------------------------------------------------------------------
 //	 | (C) 2010 - 2011 http://doyouhaobaby.net All rights reserved.
 //	 | This is not a free software, use is subject to license terms
 //  +---------------------------------------------------------------------
 //
 //  “About This File”
 //  +---------------------------------------------------------------------
 //	 | Ucenter 不良词语控制器
 //  +---------------------------------------------------------------------

*/

!defined( 'DYHB_PATH' ) && exit;

class BadwordController extends InitController{

    /**
	 * 数据查询条件过滤
	 */
    public function filter_( &$arrMap ){

		$arrMap['badword_find'] = array( 'like',"%".Safe::getGpc( 'badword_find' )."%" );
	}

    /**
     * 批量添加数据前置处理
     */
    public function many_insert(){

       $sBadwords = Safe::getGpc( 'badwords','P' );
       if( $sBadwords =='' ){

      	  $this->E( G::L( '导入词汇不能为空！') );
       }

	   $nType = Safe::getGpc( 'type','P' );

	   $oBadword = BadwordModel::F()
			           ->query();

	   if( $nType == 0 ) {

			$bResult = $oBadword->truncateBadword();

			if( $bResult === false ){

				$this->E( G::L( '清空badword数据库出错！' ) );
			}

			$nType = 1;
	   }

	   $arrValue = explode( "\n", str_replace(array("\r", "\n\n"), array("\r", "\n"),$sBadwords ) );
	   foreach( $arrValue as $sValue ) {

		   $arrValueTwo = explode( "=", $sValue );
		   $arrUserData = UserModel::M()->userData();

		   if( !isset( $arrValueTwo[1] ) ) $arrValueTwo[1] = '*';

		   $bResult = $oBadword->addBadword( $arrValueTwo[0], $arrValueTwo[1], $arrUserData['user_name'], $nType );
		   if( $bResult === false ){

				$this->E( G::L( '插入出错！' ).'<br/>'.$oBadword->getErrorMessage() );
			}
		}

		$this->S( G::L( '导入数据成功了' ) );
     }

     /**
      * 导出词汇数据
      */
     public function export( ){

   	    $arrDatas = BadwordModel::F()
   	            ->asArray()
   	            ->all()
   	            ->query();

		$sString = '';

		if( $arrDatas ) {

			foreach( $arrDatas as $arrData) {

				$sString .= $arrData['badword_find'].'='.$arrData['badword_replacement']."\r\n";
			}
		}

		header('Content-Type: text/plain');
		header('Content-Disposition: attachment; filename=Badwords.txt');
		if (preg_match("/MSIE ([0-9].[0-9]{1,2})/", $_SERVER['HTTP_USER_AGENT'])){

			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Pragma: public');
		}

		else {

			header('Pragma: no-cache');
		}

		echo $sString;
    }

}
